﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class TimeAlarm
    {
        #region Model
        private Guid __id;
        private string _practiceid;
        private int _sequence;
        private string _supportingid;
        private DateTime? _alarmstart;
        private DateTime? _alarmdeadline;
        private int? _alarmstatus;
        private DateTime? _finishdate;
        private DateTime? _resceduleddeadline;
        private DateTime? _nextstart;
        private DateTime? _nextdeadline;
        private string _nextproposer;
        /// <summary>
        /// 
        /// </summary>
        public Guid _id
        {
            set { __id = value; }
            get { return __id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string practiceID
        {
            set { _practiceid = value; }
            get { return _practiceid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int sequence
        {
            set { _sequence = value; }
            get { return _sequence; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string supportingID
        {
            set { _supportingid = value; }
            get { return _supportingid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? alarmStart
        {
            set { _alarmstart = value; }
            get { return _alarmstart; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? alarmDeadline
        {
            set { _alarmdeadline = value; }
            get { return _alarmdeadline; }
        }
        /// <summary>
        /// 0:initial
        ///11：开始时间到了，发送给相关的Owner/Reviewer
        ///12：Owner/Reviewer回复，并停止提示
        ///21：Deadline时间到了，发送给相关的Owner/Reviewer
        ///22：Owner/Reviewer回复，并停止提示
        ///71：Owner/Reviewer提议新的时间，发送给了审计管理员，待审批
        ///72：审计管理员回复，通知到Owner/Review后，重新恢复至0～22的状态
        ///81：Owner完成了，并上传提交，等待审批
        ///82：Reviewer审批完，等待审计管理员审批
        ///91：Owner完成提交，被Reviewer驳回
        ///92：Reviewer审批完，被审计管理员驳回
        ///100：提交成功。等待创建下一次提交Supporting的时间。
        ///101：结束！
        /// </summary>
        public int? alarmStatus
        {
            set { _alarmstatus = value; }
            get { return _alarmstatus; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? finishDate
        {
            set { _finishdate = value; }
            get { return _finishdate; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? resceduledDeadline
        {
            set { _resceduleddeadline = value; }
            get { return _resceduleddeadline; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? nextStart
        {
            set { _nextstart = value; }
            get { return _nextstart; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? nextDeadline
        {
            set { _nextdeadline = value; }
            get { return _nextdeadline; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string nextProposer
        {
            set { _nextproposer = value; }
            get { return _nextproposer; }
        }
        #endregion Model

        #region TimeAlarmFunction

        /// <summary>
        /// Initialize
        /// </summary>
        public TimeAlarm(Guid in_id)
        {
            if (TimeAlarm.ExistTimeAlarm(in_id) == 0)
                return;

            Model.TimeAlarm myTA = new Model.TimeAlarm();
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            myTA = optTA.GetModel(in_id);

            this.__id = myTA._id;
            this._practiceid = myTA.practiceID;
            this._sequence = myTA.sequence;
            this._supportingid = myTA.supportingID;
            this._alarmstart = myTA.alarmStart;
            this._alarmdeadline = myTA.alarmDeadline;
            this._alarmstatus = myTA.alarmStatus;
            this._finishdate = myTA.finishDate;
            this._resceduleddeadline = myTA.resceduledDeadline;
            this._nextstart = myTA.nextStart;
            this._nextdeadline = myTA.nextDeadline;
            this._nextproposer = myTA.nextProposer;
        }

        /// <summary>
        /// Count the number of TimeAlarm
        /// </summary>
        public static int CountAllTimeAlarm(string in_strWhere)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            return optTA.GetRecordCount(in_strWhere);
        }

        /// <summary>
        /// List all the Process in pages
        /// Parameters: 1.in_orderBy:"practiceID","supportingID","alarmDeadline"
        /// </summary>
        public static List<TimeAlarm> GetAllTimeAlarm(string in_strWhere, string in_orderby, int in_startIndex, int in_endIndex)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            DataSet ds = optTA.GetListByPage(in_strWhere, in_orderby, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<TimeAlarm> returnTimeAlarm = new List<TimeAlarm>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Guid temp_id = new Guid(ds.Tables[0].Rows[i]["_id"].ToString());
                TimeAlarm newTA = new TimeAlarm(temp_id);
                returnTimeAlarm.Add(newTA);
            }

            return returnTimeAlarm;
        }

        /// <summary>
        /// Check if the TimeAlarm exists
        /// Return  1:Exist;
        ///         0:Not Exist;
        /// </summary>
        public static int ExistTimeAlarm(Guid in_id)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            if (!optTA.Exists(in_id))
                return 0;
            else return 1;
        }

        /// <summary>
        /// Create a new TimeAlarm
        /// Return  1.Success
        ///         3.DAL Error
        /// </summary>
        public static int CreateTimeAlarm(string in_practiceID, int in_sequence, string in_supportingID, DateTime in_alarmStart, DateTime in_alarmDeadline)
        {
            Model.TimeAlarm myTA = new Model.TimeAlarm();
            myTA._id = Guid.NewGuid();
            myTA.practiceID = in_practiceID;
            myTA.sequence = in_sequence;
            myTA.supportingID = in_supportingID;
            myTA.alarmStart = in_alarmStart;
            myTA.alarmDeadline = in_alarmDeadline;
            myTA.alarmStatus = 0;

            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            bool ok = optTA.Add(myTA);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update the TimeAlarm
        /// Return: 1.Success
        ///         2.DAL Error
        /// </summary>
        public int UpdateTimeAlarm()
        {
            Model.TimeAlarm myTA = new Model.TimeAlarm();
            myTA.practiceID = this._practiceid;
            myTA.sequence = this._sequence;
            myTA.supportingID = this._supportingid;
            myTA.alarmStart = this._alarmstart;
            myTA.alarmDeadline = this._alarmdeadline;
            myTA.alarmStatus = this._alarmstatus;
            myTA.finishDate = this._finishdate;
            myTA.resceduledDeadline = this._resceduleddeadline;
            myTA.nextStart = this._nextstart;
            myTA.nextDeadline = this._nextdeadline;
            myTA.nextProposer = this._nextproposer;

            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            bool ok = optTA.Update(myTA);
            if (!ok) return 2;
            else return 1;
        }

        /// <summary>
        /// Delete one TimeAlarm
        /// Return: 1.Success
        ///         2.Not Exist
        ///         3.DAL Error
        /// </summary>
        public static int DeleteTimeAlarm(Guid in_id)
        {
            if (TimeAlarm.ExistTimeAlarm(in_id) == 0)
                return 2;

            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            bool ok = optTA.Delete(in_id);
            if (!ok) return 3;
            else return 1;
        }

        #endregion TimeAlarmFunction

        #region TimeAlarmFunForBiz

        /*** 需要添加 ***/

        /// <summary>
        /// 根据颜色，返回状态位
        /// Parameter:  1."red","yellow","green"
        /// </summary>
        private static List<int> Fun_GetColorStatus(string in_color)
        {
            List<int> returnInt = new List<int>();

            switch (in_color)
            {
                case "red":
                    returnInt.Add(int.Parse("21"));
                    returnInt.Add(int.Parse("22"));
                    returnInt.Add(int.Parse("91"));
                    returnInt.Add(int.Parse("92"));
                    break;
                case "yellow":
                    returnInt.Add(int.Parse("11"));
                    returnInt.Add(int.Parse("12"));
                    returnInt.Add(int.Parse("71"));
                    returnInt.Add(int.Parse("72"));
                    returnInt.Add(int.Parse("81"));
                    returnInt.Add(int.Parse("82"));
                    break;
                case "green":
                    returnInt.Add(int.Parse("0"));
                    returnInt.Add(int.Parse("100"));
                    returnInt.Add(int.Parse("101"));
                    returnInt.Add(int.Parse("102"));
                    break;
                default:
                    break;
            }

            return returnInt;
        }

        /// <summary>
        /// 根据颜色，返回状态位，文字版本
        /// Parameter:  1."red","yellow","green"
        /// </summary>
        private static string Fun_GetColorStatusSQL(string in_color)
        {
            List<int> myInt = new List<int>();
            myInt = TimeAlarm.Fun_GetColorStatus(in_color);

            string returnString;
            returnString = string.Empty;

            for (int i = 0; i < myInt.Count; i++)
            {
                if (i == 0)
                    returnString = myInt[i].ToString();
                else
                    returnString = returnString + "," + myInt[i].ToString();
            }

            return returnString;
        }

        /// <summary>
        /// 根据颜色和类型，选择TimeAlarm的个数
        /// Parameters  1.in_allProcessPracticeActivity:"all","process","practice","activity";
        ///             2.in_id:填入相应的ID，如果是"all"，则可为空;
        ///             3.in_sequence:如果是"activity"，则填写sequence;
        ///             4.in_color:填写颜色，"red","yellow","green","all"
        /// </summary>
        public static int CountTimeAlarms(string in_allProcessPracticeActivity, string in_id, int? in_sequence, string in_color)
        {
            /*** 检查输入的颜色 in_color ***/
            if (in_color != "red" && in_color != "yellow" && in_color != "green" && in_color != "all")
                return 0;

            string strWhere;
            strWhere = string.Empty;

            switch (in_allProcessPracticeActivity)
            {
                case "all":
                    if (in_color == "all")
                        strWhere = "year(alarmDeadline)=year(GETDATE())";
                    else
                        strWhere = "alarmStatus in (" + TimeAlarm.Fun_GetColorStatusSQL(in_color) + ") and year(alarmDeadline)=year(GETDATE())";
                    break;

                case "process":
                    if (in_id == null || in_id.ToString() == string.Empty)
                        return 0;
                    if (in_color == "all")
                        strWhere = "year(alarmDeadline)=year(GETDATE()) and practiceID like '" + in_id + "%'";
                    else
                        strWhere = "alarmStatus in (" + TimeAlarm.Fun_GetColorStatusSQL(in_color) + ") and year(alarmDeadline)=year(GETDATE()) and practiceID like '" + in_id + "%'";
                    break;

                case "practice":
                    if (in_id == null || in_id.ToString() == string.Empty)
                        return 0;
                    if (in_color == "all")
                        strWhere = "year(alarmDeadline)=year(GETDATE()) and practiceID='" + in_id + "%'";
                    else
                        strWhere = "alarmStatus in (" + TimeAlarm.Fun_GetColorStatusSQL(in_color) + ") and year(alarmDeadline)=year(GETDATE()) and practiceID='" + in_id + "%'";
                    break;

                case "activity":
                    if (in_id == null || in_id.ToString() == string.Empty)
                        return 0;
                    if (in_sequence == null || in_sequence.ToString() == string.Empty || int.Parse(in_sequence.ToString()) < 1)
                        return 0;
                    if (in_color == "all")
                        strWhere = "year(alarmDeadline)=year(GETDATE()) and practiceID='" + in_id + "' and sequence=" + in_sequence;
                    else
                        strWhere = "alarmStatus in (" + TimeAlarm.Fun_GetColorStatusSQL(in_color) + ")  and year(alarmDeadline)=year(GETDATE()) and practiceID='" + in_id + "' and sequence=" + in_sequence;
                    break;

                /*** 啥都不是，返回0 ***/
                default:
                    return 0;
            }

            return TimeAlarm.CountAllTimeAlarm(strWhere);
        }

        #endregion TimeAlarmFunForBiz
    }
}
